package com.example.springprinciple.controller;

import com.example.springprinciple.model.Dog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class DogBeanController {
    @Autowired
    private ApplicationContext context;
    @Autowired
    private Dog singletonDog;
    @Autowired
    private Dog prototypeDog;
    @Autowired
    private Dog requestDog;
    @Autowired
    private Dog sessionDog;
    @Autowired
    private Dog applicationDog;
    /**
     * 通过比较不同作用域，spring容器启动直接注入，后不会进行改变（原型作用域）
     * @return
     */

    @RequestMapping("/single")
    public String single(){
        //从context获取对象
        Dog contexDog = context.getBean("singletonDog",Dog.class);
        return "contextDog:"+contexDog +",autowiredDog:"+singletonDog;
    }
    @RequestMapping("/prototype")
    public String prototype(){
        //从context获取对象
        Dog contexDog = context.getBean("prototypeDog",Dog.class);

        return "contextDog:"+contexDog +",<br/> autowiredDog:"+prototypeDog;
    }


    @RequestMapping("/request")
    public String request(){
        //从context获取对象
        Dog contexDog = context.getBean("requestDog",Dog.class);
        return "contextDog:"+contexDog +",<br/> autowiredDog:"+requestDog;
    }

    @RequestMapping("/session")
    public String session(){
        //从context获取对象
        Dog contexDog = context.getBean("sessionDog",Dog.class);
        return "contextDog:"+contexDog +",<br/> autowiredDog:"+sessionDog;
    }

    @RequestMapping("/application")
    public String application(){
        //从context获取对象
        Dog contexDog = context.getBean("applicationDog",Dog.class);
        return "contextDog:"+contexDog +",<br/> autowiredDog:"+applicationDog;
    }
}
